package com.yl.ylnews.fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.yl.ylnews.MainActivity;
import com.yl.ylnews.R;
import com.yl.ylnews.base.BaseViewPager;
import com.yl.ylnews.base.imp.GovPager;
import com.yl.ylnews.base.imp.HomePager;
import com.yl.ylnews.base.imp.NewsPager;
import com.yl.ylnews.base.imp.SettingPager;
import com.yl.ylnews.base.imp.SmartPager;
import com.yl.ylnews.view.NoScrollViewPager;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/17 0017.
 */

public class MainFragment extends BaseFragment {

    private static final String TAG = "MainFragment";
    private ArrayList<BaseViewPager> mPagers;
    private NoScrollViewPager mVp_pager;
    private RadioGroup mRg_group;

    @Override
    public void initData() {
        mPagers = new ArrayList<>();
        mPagers.add(new HomePager(getActivity()));
        mPagers.add(new NewsPager(getActivity()));
        mPagers.add(new SmartPager(getActivity()));
        mPagers.add(new GovPager(getActivity()));
        mPagers.add(new SettingPager(getActivity()));

        mVp_pager.setAdapter(new BaseViewPagerAdpter());

        mVp_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPagers.get(position).initData();

                if (position == 0 || position == mPagers.size() - 1) {
                    setSlidingMenu(false);
                } else {
                    setSlidingMenu(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mRg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.btn_home:
                        mVp_pager.setCurrentItem(0, false);
                        break;
                    case R.id.btn_news:
                        mVp_pager.setCurrentItem(1, false);
                        break;
                    case R.id.btn_smart:
                        mVp_pager.setCurrentItem(2, false);
                        break;
                    case R.id.btn_gov:
                        mVp_pager.setCurrentItem(3, false);
                        break;
                    case R.id.btn_setting:
                        mVp_pager.setCurrentItem(4, false);
                        break;
                }
            }
        });
        mVp_pager.setCurrentItem(1, false);
        mPagers.get(1).initData();
        setSlidingMenu(false);

        Log.d(TAG, "initData: 初始化完毕");


    }

    public NewsPager getNewsPager(){
        return (NewsPager) mPagers.get(1);
    }

    private void setSlidingMenu(boolean enable) {
        MainActivity activity = (MainActivity) getActivity();

        if (enable) {
            activity.getSlidingMenu().setTouchModeAbove(SlidingMenu.LEFT);
        } else {
            activity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }

    }


    @Override
    public View initView() {
        View view = View.inflate(getActivity(), R.layout.fragment_content, null);
        mVp_pager = (NoScrollViewPager) view.findViewById(R.id.vp_pager);
        mRg_group = (RadioGroup) view.findViewById(R.id.rg_group);
        return view;
    }


    class BaseViewPagerAdpter extends PagerAdapter {


        @Override
        public int getCount() {
            return mPagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mPagers.get(position).mView;
            container.addView(view);



            return view;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView((View) object);
        }
    }

}
